<template>
  <div class="home">
    <button
      @click="goLogin">跳转</button>
    <div class="popup">
      <div class="modal"
        v-if="visiblePopup">
        <div
          class="modal-mask">
        </div>
        <div
          class="modal-content">
          <div
            class="modal-header">
            <h3>{{title}}</h3>
            <div>
              {{agreementInfo.agreementContent}}
            </div>
          </div>
          <div
            class="modal-body">
            <slot></slot>
          </div>
          <div
            class="modal-footer">
            <!-- <button
              @click="cancel"
              class="default">取消</button> -->
            <button
              @click="confirm"
              class="default confirm">确定</button>
          </div>
        </div>
      </div>
    </div>

    <div
      style="width: 200px; height: 30px; overflow-y: auto;">

    </div>

    <div class="title">邀请加入
    </div>
    <div class="hint">
      您是否接受xxx的邀请</div>
    <div
      class="informatization">
      <t-form ref="form"
        :data="formData"
        :rules="rules"
        reset-type="initial"
        show-error-message
        label-align="left"
        :disabled="disabled"
        @reset="onReset"
        @submit="onSubmit">
        <t-form-item
          label="姓名"
          name="name">
          <t-input
            v-model="formData.name"
            placeholder="请输入内容"></t-input>
        </t-form-item>
        <t-form-item
          label="性别"
          name="sex">
          <t-cell arrow
            :title="cityState.city.join(' ') ? cityState.city.join(' ') : '请选择性别'"
            @click="cityState.show = true" />
          <t-popup
            v-model="cityState.show"
            placement="bottom">
            <t-picker
              v-model="cityState.city"
              :columns="cityOptions"
              @confirm="onConfirm"
              @cancel="cityState.show = false"
              @pick="onPick" />
          </t-popup>
        </t-form-item>
        <t-form-item
          label="年龄"
          name="age">
          <t-input
            v-model="formData.age"
            placeholder="请输入年龄"></t-input>
        </t-form-item>
        <t-form-item
          label="手机号"
          name="name">
          <t-input
            v-model="formData.phoneNumber"
            placeholder="输入手机号码"
            :tips="tips">
            <template #suffix>
              <div
                style="display: flex; align-items: center">
                <div
                  class="suffix--line">
                </div>
              </div>
            </template>
          </t-input>
        </t-form-item>
      </t-form>
    </div>
    <div class="btn">
      <div class="default">
        不同意
      </div>
      <div
        class="default confirm"
        @click="inviteRecord">
        同意
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, toRefs, onMounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import axios from 'axios'
const router = useRouter()
const goLogin = () => {
  router.push('/login')
}
const visiblePopup: any = ref(false)
const title = ref('授权协议')
const close = () => {
  visiblePopup.value = false
}
const confirm = () => {
  signAgreement()
  close()
}

const cancel = () => {
  console.log('点击了取消')
  close()
}

const isPhoneNumber = computed(() => {
  if (/^[1][3,4,5,7,8,9][0-9]{9}$/.test(formData.phoneNumber)) {
    return ''
  }
  return 'error'
})
const tips = computed(() => {
  if (!isPhoneNumber.value) {
    return ''
  }
  return '手机号输入不正确'
})

const cityOptions = [
  [
    {
      label: '男',
      value: '男',
    },
    {
      label: '女',
      value: '女',
    },
  ],
]

const cityState = reactive({
  show: false,
  city: [],
})

const onCancel = () => {
  cityState.show = false
}

const onConfirm = (val: string[], context: number[]) => {
  console.log(val)
  console.log('context', context)
  cityState.show = false
}

const onPick = (value: any[], context: any) => {
  console.log('value', value)
  console.log('context', context)
}

const props = defineProps({
  disabled: Boolean,
})
const { disabled } = toRefs(props)

// upload

const formData = reactive({
  name: '',
  sex: '',
  age: 3,
  phoneNumber: '',
})
const form = ref(null)

// form
const onReset = () => {
  console.log('===onReset')
}

const onSubmit = (e: any) => {
  console.log('===onSubmit', e)
}

const rules = {
  name: [{ validator: (val: any) => val !== '', message: '不能为空' }],
  place: [{ validator: (val: any) => val !== '', message: '不能为空' }],
  resume: [{ validator: (val: any) => val !== '', message: '不能为空' }],
}

let userInfo = reactive({
  openId: '',
  unionId: '',
  userId: '',
})
// 获取openid
const getOpenId = (code: any) => {
  axios
    .get(`https://zg.particlesoft.info/frontend/officialAccounts?code=${code}`)
    .then((res: any) => {
      userInfo.openId = res.data.openid
      axios
        .get(
          `https://zg.particlesoft.info/frontend/officialAccountsUnionId?openId=${userInfo.openId}`
        )
        .then((res: any) => {
          userInfo.unionId = res.data.unionid
          createWechat()
        })
    })
}
//创建微信用户
const createWechat = () => {
  axios
    .post(`https://zg.particlesoft.info/frontend/api/userInfo/wechat`, {
      serviceOfficialAccountOpenid: userInfo.openId,
      miniprogramOpenid: '',
      unionid: userInfo.unionId,
    })
    .then((res: any) => {
      console.log(res)
      userInfo.userId = res.data.data.userId
      getUserInfo()
    })
}

const obj = reactive({
  wechatUnionId: '',
  wechatServiceOfficialAccountOpenId: '',
  wechatMiniprogramOpenId: '',
})
// 获取用户信息
const getUserInfo = () => {
  axios
    .get(
      `https://zg.particlesoft.info/frontend/api/userInfo?userId=${userInfo.userId}&wechatUnionId=${obj.wechatUnionId}&
  wechatServiceOfficialAccountOpenId=${userInfo.openId}&wechatMiniprogramOpenId=${obj.wechatMiniprogramOpenId}`
    )
    .then((res: any) => {
      if (!(res.data.data.signedAgreementVersion == res.data.data.currentAgreementVersion)) {
        getUserAgree(res.data.data.currentAgreementVersion)
      } else {
        visiblePopup.value = false
      }
    })
}
let agreementInfo = reactive({
  agreementContent: ' ',
  agreementId: '',
})
// 获取版本协议
const getUserAgree = (num: any) => {
  axios
    .get(`https://zg.particlesoft.info/frontend/api/agreement?version=${num}`)
    .then((res: any) => {
      agreementInfo.agreementContent = res.data.data.content
      agreementInfo.agreementId = res.data.data.agreementId
      visiblePopup.value = true
    })
}

// 签订协议
const signAgreement = () => {
  axios
    .post(`https://zg.particlesoft.info/frontend/api/agreement/sign`, {
      userId: userInfo.userId,
      agreementId: agreementInfo.agreementId,
    })
    .then((res: any) => {
      console.log(res)
    })
}

// 获取家庭成员信息
const familyId = ref('')
const familyMemberId = ref('')
const getFamilyMemberInfo = (value: any) => {
  familyMemberId.value = value
  axios
    .get(
      `https://zg.particlesoft.info/frontend/api/familyMember?familyId=${familyId.value}&familyMemberId=${value}`
    )
    .then((res: any) => {
      const list = res.data.data.filter((item: any) => item.familyMemberId == value)
      formData.name = list[0].nickName
    })
}
// 获取
const inviteRecord = () => {
  console.log(userInfo.userId)
  axios
    .post(`https://zg.particlesoft.info/frontend/api/personinfo/invite`, {
      familyMemberId: familyMemberId.value,
      userId: userInfo.userId,
      nickName: formData.name,
      // sexValue: cityState.city[0] == '男' ? 0 : 1,
      mobilePhoneNumber: formData.phoneNumber,
      age: formData.age,
      address: '',
    })
    .then((res: any) => {
      console.log(cityState.city[0])
      console.log(res, '邀请建档')
      window.location.href = 'https://zg.particlesoft.info/frontend/skipminiprogram'
    })
}

// const scrollTest = () => {
//   let wrapper: any = document.querySelector('.popup-content')
//   var options = {
//     scrollY: true, // 开启纵向滚动
//     click: true, // 支持点击事件
//     scrollbar: {
//       fade: true, // 滚动条渐隐
//     },
//   }
//   var scroll = new BScroll(wrapper, options)
// }
onMounted(() => {
  // scrollTest()
  const urlParams = new URLSearchParams(window.location.search)
  const code = urlParams.get('code')
  const familyMemberId: any = urlParams.get('familyMemberId')
  getOpenId(code)
  // 获取家庭成员信息
  getFamilyMemberInfo(familyMemberId)
  // @ts-ignore
  form.value.setValidateMessage(rules)
})
</script>

<style lang="less" scoped>
.home {
  padding: 0.3rem;
  font-size: 0.4rem;

  .title {
    font-size: 0.5rem;
    text-align: center;
    font-weight: 600;
  }

  .hint {
    font-size: 0.5rem;
    text-align: center;
    background-color: #54b6ab;
    color: #fff;
    padding: 0.15rem;
    border-radius: 0.1rem;
    margin-top: 0.3rem;
  }

  .btn {
    display: flex;
    justify-content: center;
    margin-top: 5rem;

    .default {
      border: 1px solid #54b6ab;
      padding: 0.3rem;
      margin-right: 0.5rem;
      width: 3rem;
      text-align: center;
      border-radius: 0.2rem;
      color: #54b6ab;
    }

    .confirm {
      background-color: #54b6ab;
      color: #fff;
    }
  }
}

.popup-style {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 0 0.3rem;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;

  h3 {
    text-align: center;
    padding: 0.5rem;
  }

  .confirm {
    background-color: #54b6ab;
    color: #fff;
    border: 1px solid #54b6ab;
    padding: 0.3rem;
    margin-right: 0.5rem;
    width: 80%;
    text-align: center;
    border-radius: 0.2rem;
    margin: 0.5rem 0;
  }

  .popup-content {
    height: 12.4rem;
    overflow-y: scroll;
  }
}

/deep/.t-picker-item__item--selected {
  font-weight: 600 !important;
}

/deep/.t-cell__right {
  display: none !important;
}

/deep/.t-picker-item__item {
  font-size: 0.45rem !important;
}

/deep/.t-picker-item__item--selected {
  font-weight: 800 !important;
}

/deep/.t-picker__confirm {
  color: #54b6ab;
}

.popup-style {
  font-size: 0.45rem !important;
}

/deep/.t-popup--center {
  width: 90%;
  height: 90%;
}

/deep/.t-cell::after {
  display: none;
}
/deep/.t-form .t-form__item {
  padding: 8px 8px;
}
/deep/.t-form__label {
  padding-right: 0;
  width: 60px !important;
}
.modal {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 9999;
  width: 100vw;
  height: 100vh;
}

.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
}

.modal-content {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 9rem;
  background-color: #fff;
  transform: translate(-50%, -50%);
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}
.modal-header {
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  align-items: center;
  padding-top: 0.5rem;
  div {
    margin-top: 0.3rem;
    font-size: 0.45rem;
    height: 13rem;
    padding: 0.3rem;
    overflow-y: scroll;
    width: 8.25rem;
    background-color: #54b6ab;
    text-align: justify;
    border-radius: 0.3rem;
    color: #fff;
  }
}

.modal-header h3 {
  margin: 0;
}

.modal-header button {
  border: none;
  background-color: transparent;
  cursor: pointer;
}

.modal-body {
  margin-bottom: 0.3rem;
}

.modal-footer {
  display: flex;
  justify-content: center;
  margin-bottom: 0.3rem;
  .default {
    border: 1px solid #54b6ab;
    padding: 0.3rem;
    margin-right: 0.5rem;
    width: 3rem;
    text-align: center;
    border-radius: 0.2rem;
    color: #54b6ab;
  }

  .confirm {
    background-color: #54b6ab;
    color: #fff;
    width: 70%;
  }
}
</style>
